/**
 * Created by Administrator on 2016/1/21.
 */

var ci_redis={
    options:{
        prefix:'ci_session',
        helper:'helper',
        user:'user'
    },
    session:function(client,name,fun){
        client.get(this.options.prefix+':'+name,function(err,response){
            if(err){
                console.log(name);
                console.log(response);
                console.log('read redis error for session');
                return false;
            }

            return fun(response);
        })
    },
    userRelationHelper:function(client,user_id,fun){
        client.get(this.options.user+':'+user_id, function (err,response) {
            if(err){
                console.log('read redis error for helper');
                return false;
            }
            return fun(response);
        });
    },
    helperRelationUser:function(client,helper_id,fun){
        client.get(this.options.helper+':'+helper_id, function (err,response) {
            if(err){
                console.log('read redis error for user');
                return false;
            }
            return fun(response);
        });
    },
    setUserHelper:function(client,helper,user_id){
        client.set(this.options.user+":"+user_id,JSON.stringify(helper));
    },
    setHelperUser:function(client,user,helper_id){
        client.set(this.options.helper+":"+helper_id,JSON.stringify(user));
    },
    setHelperBusy:function(client,helper_id,busy,fun){
        client.hget('helper_online',helper_id,function(err,helper){
            if(err){
                console.log('read redis error for user');
                return false;
            }
            helper.busy = busy;
            client.hset('helper_online',helper_id,JSON.stringify(helper));
            return fun(response);
        });

    }
};

exports.ci_redis=ci_redis;